﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AspNetCs.Bll;
using System.Data;
using AspNetCs.Common;
using AspNetCs.Model;
using AspNetCs.Enumerations;
using AspNetCs.Enumerations.User_Enum;
using System.Collections;
using AspNetCs.Enumerations.Channel_Enum;
using AspNetCs.Enumerations.memorial_enum;
using AspNetCs.Enumerations.SaleNumber_Enum;

public partial class Users_Create_memorial : BasePage
{
    protected static string DefaultValue = "";
    protected Memorial MemorialBll = new Memorial();
    protected Memorial_Type memorialtype = new Memorial_Type();
    protected SaleNumber SaleBll = new SaleNumber();

    protected int T = 1;//1=单，2=双
    protected void Page_Load(object sender, EventArgs e)
    {
        this.WebEditor1.UploadFolder = Base_GetUserUpFolder;

        Base_CurrPath = Base_CurrPath + " >> 会员管理中心 >> 创建纪念馆";
        CheckUserExit("Login.Aspx?ReUrl=" + configInfo.InstallDir + "Users/Create_memorial.Aspx", "", true);
        if (!Page.IsPostBack)
        {
            bind();
        }
    }
    protected void bind()
    {
        this.Repeater1.DataSource = memorialtype.GetDataTable(" and  (typeInt=" + (int)Person_TypeInt_Enum.single + " or typeInt=" + (int)Person_TypeInt_Enum.both + ") order by orderid desc");
        this.Repeater1.DataBind();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string Menum = Txt_Number.Text.ToString();   //纪念馆馆号
        if (Menum + "" == "")
        {
            ShowMsgBack("纪念馆馆号不能为空");
            Response.End();
        }
        else
        {
            if (MemorialBll.NumberIsBeing(Menum))
            {
                ShowMsgBack("当前纪念馆馆号已经存在");
                Response.End();
            }
            if (SaleIsBegin(Menum) == -1)
            {
                ShowMsgBack("您不能使用当前纪念馆馆号");
                Response.End();
            }
        }
        memorial_model md = new memorial_model();
        md.CreateId = Base_GetLoginInfo.GetUserID;

        md.IsCheck = 1;
        md.IsIndexTop = 0;
        md.IndexTop_Begintime = DateTime.Now;
        md.IndexTop_Endtime = DateTime.Now;
        md.NetIp = StringHelper.Ip();
        md.Memorial_Type = StringHelper.isNumber_int(Typeid.Value);

        md.NumId = Menum;
        md.Names = StringHelper.HtmlEncrypt(Txt_Name.Text.ToString().Trim());

        if (md.Names == "")
        {
            ShowMsgBack("纪念馆名称不能为空");
            Response.End();
        }

        md.Surname = StringHelper.HtmlEncrypt(Txt_UserNa.Text.ToString().Trim());
        md.UserName = StringHelper.HtmlEncrypt(Txt_UserName.Text.ToString().Trim());
        md.OtherName = StringHelper.HtmlEncrypt(Txt_otherName.Text.ToString().Trim());
        md.Sex = Radio_Sex.SelectedValue;
        md.Picture = StringHelper.HtmlEncrypt(Txt_UpImg.Text.ToString().Trim());
        if (md.Picture == "")
        {
            md.Picture = "images/nopic.jpg";
        }

        md.Nation = StringHelper.HtmlEncrypt(Txt_Race.Text.ToString().Trim());
        md.Place = StringHelper.HtmlEncrypt(Txt_place.Text.ToString().Trim());
        md.Occupation = StringHelper.HtmlEncrypt(Txt_Occu.Text.ToString().Trim());
        md.Belief = StringHelper.HtmlEncrypt(Txt_Belief.Text.ToString().Trim());
        md.BirthType = DD_TimeType.SelectedValue;

        if (md.BirthType == TimeType_enum.pub.ToString())
        {
            md.Birthday = StringHelper.HtmlEncrypt(Txt_birthyear.Text.ToString().Trim());
            if (StringHelper.isDatetime_Bool(md.Birthday))
            {
                md.LunarCalendar_Birth = StringHelper.ChineseLunisolarDate(StringHelper.isDatetime(md.Birthday));
            }

        }
        else
        {
            md.LunarCalendar_Birth = StringHelper.HtmlEncrypt(Txt_birthyear.Text.ToString().Trim());
        }



        md.DepartType = DD_DeathType.SelectedValue;

        if (md.DepartType == TimeType_enum.pub.ToString())
        {
            md.DepartTime = StringHelper.HtmlEncrypt(Txt_DeathTime.Text.ToString().Trim());

            if (StringHelper.isDatetime_Bool(md.DepartTime))
            {
                md.LunarCalendar_depart = StringHelper.ChineseLunisolarDate(StringHelper.isDatetime(md.DepartTime));
            }
        }
        else
        {
            md.LunarCalendar_depart = StringHelper.HtmlEncrypt(Txt_DeathTime.Text.ToString().Trim());
        }




        md.Surname_two = StringHelper.HtmlEncrypt(Txt_SnameTwo.Text.ToString().Trim());
        md.UserName_two = StringHelper.HtmlEncrypt(Txt_unameTwo.Text.ToString().Trim());
        md.OtherName_two = StringHelper.HtmlEncrypt(Txt_OtherNameTwo.Text.ToString().Trim());
        md.Sex_two = Txt_SexTwo.SelectedValue;
        md.Picture_two = StringHelper.HtmlEncrypt(Txt_UppicTwo.Text.ToString().Trim());
        if (md.Picture_two == "")
        {
            md.Picture_two = "images/nopic.jpg";
        }
        md.Nation_two = StringHelper.HtmlEncrypt(Txt_RaceTwo.Text.ToString().Trim());
        md.Place_two = StringHelper.HtmlEncrypt(Txt_placeTwo.Text.ToString().Trim());
        md.Occupation_two = StringHelper.HtmlEncrypt(Txt_OccuTwo.Text.ToString().Trim());
        md.Belief_two = StringHelper.HtmlEncrypt(Txt_BeliefTwo.Text.ToString().Trim());



        md.BirthType_Two = Txt_birChangeTwo.SelectedValue;

        if (md.BirthType_Two == TimeType_enum.pub.ToString())
        {
            md.Birthday_two = StringHelper.HtmlEncrypt(Txt_birthTwo.Text.ToString().Trim());

            if (StringHelper.isDatetime_Bool(md.Birthday_two))
            {
                md.LunarCalendar_Birth_two = StringHelper.ChineseLunisolarDate(StringHelper.isDatetime(md.Birthday_two));
            }


        }
        else
        {
            md.LunarCalendar_Birth_two = StringHelper.HtmlEncrypt(Txt_birthTwo.Text.ToString().Trim());
        }

        md.DepartType_Two = DD_DeathTimeTwo.SelectedValue;

        if (md.DepartType_Two == TimeType_enum.pub.ToString())
        {
            md.DepartTime_two = StringHelper.HtmlEncrypt(Txt_DeathTime.Text.ToString().Trim());

            if (StringHelper.isDatetime_Bool(md.DepartTime_two))
            {
                md.LunarCalendar_depart_two = StringHelper.ChineseLunisolarDate(StringHelper.isDatetime(md.DepartTime_two));
            }

        }
        else
        {
            md.LunarCalendar_depart_two = StringHelper.HtmlEncrypt(Txt_DeathTime.Text.ToString().Trim());
        }

        md.IsEncrypt = StringHelper.isNumber_int(IsPwd.SelectedValue);
        if (md.IsEncrypt == 1)
        {
            md.GetPwd = EncryptString.GetEncrypt(Txt_Pwdchange.Text);
        }
        if (Request["provice"] != null)
        {
            md.Province = StringHelper.isNumber_int(Request["provice"].ToString());

        }
        if (md.Province > 0)
        {
            if (Request["city"] != null)
            {
                md.City = StringHelper.isNumber_int(Request["city"].ToString());
            }
        }
        md.ReadMe = WebEditor1.Text;
        md.TempleId = StringHelper.isNumber_int(Request["templateId"]); ;//模板ID留到添加模板的时候做 2009-10-21

        string InfoMsg = "";
        DrePoint();
        if (MemorialBll.Add(md, out InfoMsg) > 0)
        {
            ResultStr("memorial_List.aspx", InfoMsg, true);
        }
        else
        {
            ShowMsgBack(InfoMsg);
        }

    }

    /// <summary>
    /// 出售中是否存在该馆号，并且是否该用户可以使用
    /// </summary>
    /// <param name="Menum"></param>
    /// <returns></returns>
    protected int SaleIsBegin(string Menum)
    {
        int Result = 0;//可以使用 
        DataTable dt = SaleBll.GetDataTable(" and  Number='" + Menum + "'");
        if (dt.Rows.Count > 0)
        {
            Result = -1;//表中存在该馆号,但不一定是该用户的
            foreach (DataRow dr in dt.Rows)
            {
                if (StringHelper.isNumber_int(dr["UserId"].ToString()) == Base_GetLoginInfo.GetUserID && StringHelper.isNumber_int(dr["UserState"].ToString()) == (int)UserState_Enum.Yes)
                {
                    Result = 1;//表示可以使用
                    break;
                }
            }
        }
        return Result;
    }

    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemIndex != -1)
        {
            DataRowView drv = (DataRowView)e.Item.DataItem;
            Button btn = e.Item.FindControl("btnCreate") as Button;
            if (Base_GetUserInfo.Point >= StringHelper.isNumber_int(drv["TypePoint"].ToString()))
            {
                btn.CommandArgument = drv["id"].ToString();
                btn.CommandName = "Create";
            }
            else
            {
                btn.Enabled = false;
                btn.Text = "请您充值";
            }
        }
    }
    protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "Create")
        {



            int id = StringHelper.isNumber_int(e.CommandArgument.ToString());
            Memorial_Type_Model MemorialModel = memorialtype.GetModel(id);

            Base_CurrPath = Base_CurrPath + " >> " + MemorialModel.TypeName;
            this.Panel1.Visible = false;
            this.Panel2.Visible = true;
            if (MemorialModel.TypeInt == (int)Person_TypeInt_Enum.single)
            {
                T = (int)Person_TypeInt_Enum.single;
                this.TwoPanel.Visible = false;
            }
            else
            {
                T = (int)Person_TypeInt_Enum.both;
            }

            Typeid.Value = MemorialModel.Id.ToString();
            lb_Type.Text = "<span style='font-size:12px; color:#DE575E'>" + MemorialModel.TypeName + "</span>(所需点数:" + MemorialModel.TypePoint + "点)";

            EnumDescription[] Sex_Ed = EnumDescription.GetFieldTexts(typeof(Sex_Enum), EnumDescription.SortType.Rank);
            EnumDescription[] Ed = EnumDescription.GetFieldTexts(typeof(TimeType_enum), EnumDescription.SortType.Rank);


            this.Radio_Sex.DataTextField = "EnumDisplayText";
            this.Radio_Sex.DataValueField = "EnumDisplayText";
            this.Radio_Sex.DataSource = Sex_Ed;
            this.Radio_Sex.DataBind();
            this.Radio_Sex.SelectedIndex = 0;
            this.Radio_Sex.RepeatColumns = 5;

            this.Txt_SexTwo.DataTextField = "EnumDisplayText";
            this.Txt_SexTwo.DataValueField = "EnumDisplayText";
            this.Txt_SexTwo.DataSource = Sex_Ed;
            this.Txt_SexTwo.DataBind();
            this.Txt_SexTwo.SelectedIndex = 0;
            this.Txt_SexTwo.RepeatColumns = 5;





            this.DD_TimeType.DataTextField = "EnumDisplayText";
            this.DD_TimeType.DataValueField = "EnumValueStr";
            this.DD_TimeType.DataSource = Ed;
            this.DD_TimeType.DataBind();
            this.DD_TimeType.SelectedIndex = 0;

            this.DD_DeathType.DataTextField = "EnumDisplayText";
            this.DD_DeathType.DataValueField = "EnumValueStr";
            this.DD_DeathType.DataSource = Ed;
            this.DD_DeathType.DataBind();
            this.DD_DeathType.SelectedIndex = 0;


            this.Txt_birChangeTwo.DataTextField = "EnumDisplayText";
            this.Txt_birChangeTwo.DataValueField = "EnumValueStr";
            this.Txt_birChangeTwo.DataSource = Ed;
            this.Txt_birChangeTwo.DataBind();
            this.Txt_birChangeTwo.SelectedIndex = 0;

            this.DD_DeathTimeTwo.DataTextField = "EnumDisplayText";
            this.DD_DeathTimeTwo.DataValueField = "EnumValueStr";
            this.DD_DeathTimeTwo.DataSource = Ed;
            this.DD_DeathTimeTwo.DataBind();
            this.DD_DeathTimeTwo.SelectedIndex = 0;
            ArrayList list = new ArrayList();
            DataTable dt = SaleBll.GetDataTable(" and SaleType=" + (int)Channel_Type.memorial + "  and  UserId=0");
            foreach (DataRow dr in dt.Rows)
            {
                list.Add(dr["Number"]);
            }
            DefaultValue = MemorialBll.RndNum(list, RndLen);
            this.Txt_Number.Text = DefaultValue;

        }
    }

    /// <summary>
    /// 扣除点数
    /// </summary>
    protected void DrePoint()
    {
        Users userBll = Users_Bll.CreatNew;
        int UserId = Base_GetUserInfo.UserId;
        int TemplatePoint = StringHelper.isNumber_int(Request["hldTemplePoint"]);
        int TempId = StringHelper.isNumber_int(Request["templateId"]);
        if (UserId != 0)
        {
            if (TemplatePoint != 0)
            {
                userBll.DrePoint(TemplatePoint, UserId);
            }
        }
        else
        {
            CheckUserExit("Login.Aspx?ReUrl=" + configInfo.InstallDir + "Users/CreateAncestralHall.Aspx", "请您登录", true);
        }
    }
    protected string TemplateList()
    {
        string ResultStr = "";
        AspNetCs.Bll.Temple tempBll = AspNetCs.Bll.TempleBll.CreateNew;
        int TypeId = StringHelper.isNumber_int(Typeid.Value);
        Memorial_Type memoBLL = Memorial_TypeBll.CreateNew;
        int drePoint = StringHelper.isNumber_int(memoBLL.GetModel(TypeId).TypePoint);
        int templecount = 0;
        ResultStr = tempBll.TemplateList(AspNetCs.Enumerations.Channel_Enum.Channel_Type.memorial, TypeId, (int)Base_GetUserInfo.Point, drePoint, out templecount);
        if (templecount <= 0)
        {
            //Button1.Enabled = false;
            //Button1.Text = "必须存在模板，才能创建!";
        }
        return ResultStr;
    }

}
